package com.stud.common.enums;

import com.stud.common.constant.CycleConstants;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;

import java.time.format.DateTimeFormatter;

/**
 * @author Stud
 * @title X轴类型枚举
 * @desription XXXXX
 * @data 2022-11-16 17:39
 */
@Schema(description = "时间轴单位")
@Getter
public enum AxisEnum {
    MINUTE(1, "分钟", "%Y-%m-%d %H:%i", "YYYY-MM-DD HH24:MI", CycleConstants.PATTERN_MINUTE),
    HOUR(2, "小时", "%Y-%m-%d %H", "YYYY-MM-DD HH24", CycleConstants.PATTERN_HOUR),
    DAY(3, "天", "%Y-%m-%d", "YYYY-MM-DD", CycleConstants.PATTERN_DAY),
    WEEK(4, "周", "%x-%v", null, null),
    MONTH(5, "月", "%Y-%m", "YYYY-MM", CycleConstants.PATTERN_MONTH),
    QUARTER(6, "季", "quarter", "YYYY-Q", null),
    YEAR(7, "年", "%Y", "YYYY", CycleConstants.PATTERN_YEAR),
    ;

    private final int num;
    private final String name;
    private final String mysqlFormat;
    private final String postgresFormat;
    private final DateTimeFormatter dateTimeFormatter;

    AxisEnum(int num, String name, String mysqlFormat, String postgresFormat, DateTimeFormatter dateTimeFormatter) {
        this.num = num;
        this.name = name;
        this.mysqlFormat = mysqlFormat;
        this.postgresFormat = postgresFormat;
        this.dateTimeFormatter = dateTimeFormatter;
    }

    public String toMySql(String field) {
        if (QUARTER.equals(this)) {
            return " CONCAT(year(" + field + "), '-', quarter(" + field + ")) ";
        }
        return " date_format(" + field + ",'" + mysqlFormat + "') ";
    }

    public String toPostgresSql(String field, Integer monthStartDay) {
        //依赖自定义函数year_month,52万条数据耗时3秒左右.
        if (MONTH.equals(this) && monthStartDay != null && monthStartDay != 0 && monthStartDay != 1) {
            return " year_month(" + field + "," + monthStartDay + ") ";
        }

        return toPostgresSql(field);
    }

    public String toPostgresSql(String field) {
        //依赖自定义函数year_week,52万条数据耗时3秒左右.
        if (WEEK.equals(this)) {
            return " year_week(" + field + ") ";
        }

        return " to_char(" + field + ",'" + postgresFormat + "') ";
    }
}
